<?php

/**
 * LinkTeacherProgram filter form.
 *
 * @package    sf_sandbox
 * @subpackage filter
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormFilterTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class LinkTeacherProgramReportFormFilter extends BaseLinkTeacherProgramFormFilter {

    public function configure() {
        /*
          $this->widgetSchema['teacher'] = new sfWidgetFormChoice(array(
          'choices'          => array(),
          'renderer_class'   => 'sfWidgetFormJQueryAutocompleter',
          'renderer_options' => array('url' => 'program_teacher_report/autocomplete_script'),
          )) */;

        $choices = array('' => '');
        foreach (LinkTeacherProgramTable::$assign_status_arr as $val => $txt) {
            $choices[$val] = $txt;
        }
        $this->widgetSchema['assign_status'] = new sfWidgetFormChoice(array('choices' => $choices));
        $this->validatorSchema['assign_status'] = new sfValidatorChoice(array('choices' => array_keys(LinkTeacherProgramTable::$assign_status_arr), 'required' => false));

        $this->widgetSchema['teacher_id'] = new sfWidgetFormFilterInput(array('with_empty' => false));
        $this->validatorSchema['teacher_id'] = new sfValidatorPass(array('required' => false));

        $this->useFields(array('assign_status', 'teacher_id'));
    }

    public function getFields() {
        return array(
            'assign_status' => 'Enum',
            'teacher_id' => 'Number'
        );
    }

    public function doBuildQuery(array $values) {
        $q = parent::doBuildQuery($values);

        // --------------

        $user = sfContext::getInstance()->getUser();

        if ($user->hasAttribute('kts_global_program_filter') && is_array($data = $user->getAttribute('kts_global_program_filter'))) {
            $semister_map = array(1 => 'first', 2 => 'second');
            $q->innerJoin('r.Program p');
            $q->addWhere('p.semester = ?', $semister_map[$data['semester']]);
            $q->addWhere('p.academic_year_id = ?', $data['year']);
        }

        // --------------

        return $q;
    }

}
